// ----------------- ccayPopBox ---------------------
/**
 * @description 初始化ccayPopBox标签 $("input:text.ccayPopBox")
 * @return {JqDom} 对象本身
 */
Ccay.UI.Popbox = {};
(function(){
	$.fn.ccayPopBox = function(conf) {
		// 定义suggest的this为input,定义suggest和popup各自属性,默认分隔符
		var input = this, confSuggest = conf.suggestOp || {}, confPopop = conf.popOp || {};
		// 为suggest和popup赋予基础属性
		confSuggest.queryParam = typeof conf.queryParam == "function" ? conf.queryParam : $.extend(true, {}, conf.queryParam);
		confPopop.queryParam = typeof conf.queryParam == "function" ? conf.queryParam : $.extend(true, {}, conf.queryParam);
		confSuggest.valueField = confPopop.valueField = conf.valueField;
		confSuggest.displayField = confPopop.displayField = conf.displayField;
		confSuggest.multi = confPopop.isMultiSelect = conf.multi || false;
		conf.callback && (confPopop.isMultiSelect ? (confSuggest.fnSelected = conf.callback): (confSuggest.fnChanged = conf.callback),confPopop._callback=conf.callback);
		//定义suggest的分隔符
		var valueSplit = confSuggest.valueSplit || Ccay.Config.Format.valueSplit,
			multiSplit = confSuggest.multiSplit || Ccay.Config.Format.textSplit;
		//生成联想框
		helper.buildSuggest(confSuggest,this);
		//重写弹出框的callback
		confPopop.beforeClose = function(){
			//input.manager().disabled(true);
			input.manager().lock(input,false);
			//修复联想弹出框未锁定input导致值未刷新的问题 
			input.data("ctr_locked", false).focus();
			if(conf.box){
				conf.box.$ctr_locked=false;
			}
		}
		confPopop.callback = function(data) {
			helper.popopCallback(confPopop,data,input,valueSplit,multiSplit);
			//input.manager().disabled(true);
			input.manager().lock(input,false);
		};
		// 用户单击弹出框按钮触发的事件
		this.nextAll(".popup-trigger").first().click(
			function(){helper.buildPopop(input,confPopop,valueSplit,multiSplit,conf);}
		);
	};
	var helper = {
		buildSuggest : function(confSuggest,pThis){
//			//debugger;
//			pThis.nextAll("a").each(function(){
//				var a = $(this);
//				var marginLeft = a.css("margin-left");
//				if(marginLeft){
//					a.css("margin-left",(a.css("margin-left").replace("px","") * 1 - 17)+"px");
//				}
//			});
			pThis.after('<a class="popup-trigger" />');
			pThis.ccaySuggest(confSuggest);
			//设置特有的class样式
			pThis.addClass("ccay-suggest ccay-dropdown ccay-popbox");
		},
		popopCallback : function(confPopop,data,input,valueSplit,multiSplit){
			var callbackValue="",callbackText="";
			// 根据回调函数，得到选中的值，再复制给文本框
			if (confPopop.isMultiSelect) {
				var datasVarr = [], datasDarr = [];
				$.each(data, function() {
							datasVarr.push(Ccay.DataHelper.getObj(
									confPopop.valueField, this));
							datasDarr.push(Ccay.DataHelper.getObj(
									confPopop.displayField, this));
						});
				callbackValue = datasVarr.join(valueSplit);
				callbackText = datasDarr.join(multiSplit);
				//confPopop._callback && confPopop._callback(callbackValue,callbackText);6/19
				
			} else {
				callbackValue = Ccay.DataHelper.getObj(confPopop.valueField, data);
				callbackText = Ccay.DataHelper.getObj(confPopop.displayField, data);
			}
			//添加 返回所有数据
			confPopop._callback && confPopop._callback(callbackValue,callbackText,data);
			input.manager().setValue(callbackValue, callbackText);
		},
		buildPopop : function(input,confPopop,valueSplit,multiSplit,conf) {
			/**
			 * 将联想框中选中的数据带入到弹出框中，分为单选和多选 当用户在联想框输入一半值，点击弹出框，会将输入的带入到弹出框做查询条件
			 */
			//终止联想下拉框的联想操作
			//input.manager().disabled(false);
			//修复联想弹出框未锁定input导致值未刷新的问题 
			input.data("ctr_locked", true);
			input.manager().lock(input,true);
			var selectVal = input.manager().getValue();
			confPopop.defaultParam = {};
			if (confPopop.isMultiSelect) {
				confPopop.selectedItems = selectVal.split(valueSplit);
				var writingText = input.manager().getText().split(multiSplit).pop();
				if ($.trim(writingText).length > 0) {
					confPopop.defaultParam[confPopop.displayField] = writingText;
				}
			} else {
				confPopop.selectedItems = selectVal;
				var getText = input.manager().getText();
				if (selectVal.length == 0 && $.trim(getText).length > 0) {
					confPopop.defaultParam[confPopop.displayField] = getText;
				}
			}
			if(conf.box){
				conf.box.$ctr_locked=true;
			}
			// 调用popup 
			confPopop.igrid = true;
			Ccay.Component.popup(confPopop, confPopop.title, confPopop.isMultiSelect,
					confPopop.width, confPopop.height);
		}	
	}; 
})(Ccay.UI.Popbox);
